package com.gopay.bis.manager.bank.Wap;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2cMessage;
import com.gopay.common.domain.bank.boc.ebank.BocDayEndMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 
 * 中国银行支付  WAPB2C 对象封装
 * @BocWapB2cManager.java
 * @author xiaojian
 */
public class BocWapB2cManager implements WapPayManagerRemoteService {

	 @Resource(name = "bocConfig")
	 private Map<String, String> bocConfig;
	 
	 @Resource(name = "bocB2cBankServiceClient")
	 private BankService bocB2cBankServiceClient;
	 
	 /**
	  * B2C签名
	  * @param message
	  * @return message
	 */
	public Object sign(BaseMessage msg) throws GopayException {
		BocB2cMessage message = (BocB2cMessage) msg;
		message.setBankUrl(message.getPayUrl());
        message.setMerchantNo(message.getMerID());
		message.setPayType(1);
//		message.setBankOrderId(message.getOrderId());
		String curcode = (String) bocConfig.get("BOC.CURTYPE.CNY");//设置订单人民币种
		message.setCurCode(curcode);
			// 交易金额保留小数位后两位(0.00)
			DecimalFormat df = new DecimalFormat("0.00");
			String amount = df.format(message.getAmount());
		message.setAmount(new BigDecimal(amount));
		message.setOrderDate(message.getOrderDate());
		message.setOrderNote("gopay");
		message.setOrderUrl(message.getPayRuturnUrl());
		message.setPlainMessage(buildPlain(message));
		BankLogUtils.printB2CManagerSignSendLog(message);
		String signstr = (String) bocB2cBankServiceClient.sign(message);
        message.setSignMessage(signstr);
    	BankLogUtils.printB2CManagerSignReturnLog(message);
        return message;
	}
	
	/**
     * B2C签名明文
     * @param message
     * @return BocB2cMessage message
     */
    private String buildPlain(BocB2cMessage message) {
        StringBuffer sb = new StringBuffer();

        sb.append(StringUtils.trimToEmpty(message.getOrderId())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getOrderDate())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getCurCode())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getAmount().toString())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getMerchantNo()));

        return sb.toString();
    }

    /**
	 * 中国银行WAP验签
	 * @param message
	 * @return  boolean flag
	 */
	public Object verify(BaseMessage msg) throws GopayException {
		BocB2cMessage message = (BocB2cMessage) msg;
		StringBuffer sb = new StringBuffer("");
        sb.append(message.getMerchantNo()).append("|");
        sb.append(message.getOrderId()).append("|");
        sb.append(message.getOrderSeq()).append("|");
        sb.append(message.getCardTyp()).append("|");
        sb.append(message.getPayTime()).append("|");
        sb.append(message.getOrderStatus()).append("|");
        sb.append(message.getPayAmount());
        message.setPlainMessage(sb.toString());
    	BankLogUtils.printB2CManagerVerifySendLog(message);
    	boolean flag;
           flag = (Boolean) bocB2cBankServiceClient.verify(message);
    	BankLogUtils.printB2CManagerVerifyReturnLog(message, flag);
        return flag;
	}

	/**
	 * 中行WAP订单查询
	 * @param obj
	 * @return message
	 */
	public Object query(Object obj) throws GopayException {
		BocB2cMessage message = (BocB2cMessage) obj;
		message.setMerchantNo(message.getMerID2nd());
        // 中行借贷分离判断
        if (!message.isSupportCredit()) {
            message.setMerchantNo(message.getMerID());
        }
        message.setBankUrl(message.getQueryUrl());
    	BankLogUtils.printB2CManagerQuerySendLog(message,message);
        message = (BocB2cMessage) this.bocB2cBankServiceClient.query(message);
    	BankLogUtils.printB2CManagerQueryReturnLog(message);
        return message;
	}

	/**
     *  中行WAP日终信息拼装
     * @author xiaojian
     * @param base dayEndMsg : merchantNo fileDate ticketUrl downFileUrl
     * @throws Exception 
     */
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		BocDayEndMessage dayEndMsg = new BocDayEndMessage();
		BeanUtils.copyProperties(base, dayEndMsg);
		dayEndMsg.setMerchantNo(bocConfig.get("BOC.B2C.MERCHANTNO"));
		dayEndMsg.setMerID(bocConfig.get("BOC.B2C.MERCHANTNO"));
		dayEndMsg.setTicketUrl(bocConfig.get("BOC.B2C.GETTICKETURL"));
		dayEndMsg.setDownFileUrl(bocConfig.get("BOC.B2C.DOWNFILEURL"));
		dayEndMsg.setOutStlmCd("BOC");
		BankLogUtils.printB2CManagerDayendReqLog(dayEndMsg);
		@SuppressWarnings("unchecked")
		List<BaseDayendEntry> dayendList = (List<BaseDayendEntry>) bocB2cBankServiceClient.dayend(dayEndMsg);
		dayEndMsg .setDayendList(dayendList);
		BankLogUtils.printB2CManagerDayendReturnLog(dayEndMsg);
		return dayEndMsg;
	}

}
